import IntegrationTable, {
	IntegrationTableProps,
	OperatingItem,
} from "@/components/IntegrationTable";
import { ProColumns } from "@ant-design/pro-table";
import React, { useEffect, useState } from "react";
import { Button, message, Popconfirm, Space, Image, Cascader } from "antd";
import Search from "antd/lib/input/Search";
import { user, classify, remove } from "./service";
import { arrayToObject, getArray } from "phinney-toolkit";
import { DynamicFormItem } from "@/components/DynamicForm";
import axios from "axios";
// @page 商家
const Business: React.FC<any> = () => {
	// 组件是否已经卸载
	let isUnMounted = false;
	// 用户列表
	let userList: any[] = [];
	// 分类列表
	const classifyTypeList = [
		{ label: "商城", value: 100 },
		{ label: "二手闲置", value: 200 },
		{ label: "回收物", value: 300 },
		{ label: "好物", value: 400 },
		{ label: "游戏", value: 500 },
	];

	let [classifyList, setClassifyList] = useState<any>([]);

	const getClassifyList = async (classifyType: any) => {
		const res = await classify({
			classifyType,
		});
		return res;
	};

	// 表格项
	const columns: ProColumns[] = [
		{
			title: "分类编号",
			dataIndex: "classifyCode",
			search: false,
		},
		{
			title: "分类图片",
			dataIndex: "classifyIcon",
			search: false,
			render: (_, record: any) => {
				if (record.classifyIcon) {
					return (
						<Image.PreviewGroup>
							<Image
								src={record.classifyIcon}
								style={{ padding: 8 }}
								width={50}
								alt=""
							/>
						</Image.PreviewGroup>
					);
				} else {
					return <div>-</div>;
				}
			},
		},
		{
			title: "分类名字",
			dataIndex: "classifyName",
			search: false,
		},
		{
			title: "分类类型",
			dataIndex: "classifyType",
			valueEnum: {
				100: "商城",
				200: "二手闲置",
				300: "回收物",
				400: "好物",
				500: "游戏",
			},
		},
		{
			title: "分类级别",
			dataIndex: "level",
			search: false,
		},
		{
			title: "上级分类id",
			dataIndex: "parentId",
			search: false,
		},
		{
			title: "上级分类名称",
			dataIndex: "parentName",
			search: false,
		},
	];

	// 表单项
	const getFormItems = (vm: any, isEdit?: boolean): DynamicFormItem[] => {
		const { coupon } = vm.state;
		return [
			{
				type: "upload",
				label: "分类图片",
				name: "classifyIcon",
				fieldProps: {
					limit: 1,
				},
				required: true,
			},
			{
				type: "text",
				label: "分类名称",
				name: "classifyName",
				required: true,
			},
			{
				type: "select",
				label: "分类类型",
				name: "classifyType",
				required: true,
				options: classifyTypeList,
				fieldProps: {
					onChange: async (item: any, form: any) => {
						let res = await getClassifyList(item);
						vm.setState({
							classsifyList: res.data,
						});
					},
				},
			},
			{
				type: "custom",
				label: "上级分类",
				name: "parentId",
				render: (form: any, record: any) => {
					return (
						<div>
							<Cascader
								defaultValue={[record.parentName] || ""}
								options={vm.state.classsifyList}
								fieldNames={{
									label: "classifyName",
									value: "id",
									children: "list",
								}}
								changeOnSelect
								onChange={(item) => {
									form.setFieldsValue({
										parentId: item[item.length - 1],
									});
								}}
								placeholder="请选择类别~"
							/>
						</div>
					);
				},
			},
		];
	};

	// 一体化表格props
	const tablePorps: IntegrationTableProps = {
		rowKey: "classifyCode",
		scroll: { x: 1000 },
		columns,
		listProps: {
			url: "/classify/list",
			method: "GET",
			requestType: "params",
			paramsHandle: (params: any) => {
				return {
					...params,
				};
			},
		},
		addProps: {
			url: "/classify/add",
			modalProps: {
				formProps: {
					labelCol: { span: 5 },
				},
				formItemsHandle: (_: any, vm: any) => {
					return getFormItems(vm);
				},
			},
			paramsHandle: ({ ...params }): any => {
				params.classifyIcon = (
					params?.classifyIcon?.map((item: any) => item.url) || []
				).join(",");
				return {
					...params,
				};
			},
		},
		updateProps: {
			url: "/classify/edit",
			method: "POST",
			modalProps: {
				formProps: {
					labelCol: { span: 5 },
				},
				content: {
					onInit: async (vm) => {
						console.log(vm.state.formValues.classifyType);
						let res = await getClassifyList(vm.state.formValues.classifyType);
						vm.setState({
							classsifyList: res.data,
						});
					},
				},
				formItemsHandle: (_: any, vm: any) => {
					return getFormItems(vm, true);
				},
				formValuesHandle: async (record: any) => {
					return {
						...record,
						classifyIcon: record.classifyIcon
							? [
									{
										uid: "-1",
										status: "done",
										name: record.classifyIcon,
										url: record.classifyIcon,
									},
							  ]
							: [],
					};
				},
			},
			paramsHandle: ({ ...params }): any => {
				params.classifyIcon = (
					params?.classifyIcon?.map((item: any) => item.url) || []
				).join(",");
				return {
					...params,
				};
			},
		},
		// deleteProps: {
		// 	url: "/classify/del",
		// 	method: "POST",
		// },
	};

	// 初始化加载数据
	useEffect(() => {
		// getClassifyList();
		return () => {
			isUnMounted = true;
		};
	}, []);

	return <IntegrationTable {...tablePorps} />;
};

export default Business;
